package cn.iocoder.yudao.module.dbit.service.jobinfo;


import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.module.dbit.dal.mysql.jobresumeinfo.JobResumeInfoMapper;
import cn.iocoder.yudao.module.dbit.enums.jobinfo.JobInfoEunums;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.dbit.controller.admin.jobinfo.vo.*;
import cn.iocoder.yudao.module.dbit.dal.dataobject.jobinfo.JobInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.dbit.dal.mysql.jobinfo.JobInfoMapper;
//import cn.iocoder.yudao.module.dbit.enums.ErrorCodeConstants;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dbit.enums.ErrorCodeConstants.*;

/**
 * 企业职位信息 Service 实现类
 *
 * @author DaBen
 */
@Service
@Validated
public class JobInfoServiceImpl implements JobInfoService {

    private static final Logger log = LoggerFactory.getLogger(JobInfoServiceImpl.class);
    @Resource
    private JobInfoMapper jobInfoMapper;
    @Resource
    private JobResumeInfoMapper jobResumeInfoMapper;

    @Override
    public Integer createJobInfo(JobInfoSaveReqVO createReqVO) {
        // 插入
        JobInfoDO jobInfo = BeanUtils.toBean(createReqVO, JobInfoDO.class);
        jobInfoMapper.insert(jobInfo);
        // 返回
        return jobInfo.getJobId();
    }

    @Override
    public void updateJobInfo(JobInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateJobInfoExists(updateReqVO.getJobId());
        // 更新
        JobInfoDO updateObj = BeanUtils.toBean(updateReqVO, JobInfoDO.class);
        // 判断该职位是否被修改为薪资面议
        jobInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteJobInfo(Integer id) {
        // 校验存在
        validateJobInfoExists(id);
        // 删除
        jobInfoMapper.deleteById(id);
    }

    private void validateJobInfoExists(Integer id) {
        if (jobInfoMapper.selectById(id) == null) {
            throw exception(JOB_INFO_NOT_EXISTS);
        }
    }

    @Override
    public JobInfoDO getJobInfo(Integer id) {
        return jobInfoMapper.selectById(id);
    }

    @Override
    public PageResult<JobInfoRespVO> getJobInfoPage(JobInfoPageReqVO pageReqVO) {
        PageResult<JobInfoDO> jobInfoPage = jobInfoMapper.selectPage(pageReqVO);
        List<JobInfoDO> jobInfoList = jobInfoPage.getList();
        List<JobInfoRespVO> result = new ArrayList<>();
        for (JobInfoDO jobInfo : jobInfoList) {
            JobInfoRespVO jobInfoRespVO = BeanUtils.toBean(jobInfo, JobInfoRespVO.class);
            List<Integer> cityIds = Arrays.stream(jobInfo.getJobCity().split(";"))
                    .map(Integer::parseInt)
                    .collect(Collectors.toList());
            ArrayList<String> cityNames = new ArrayList<>();
            cityIds.forEach(id -> {
                cityNames.add(AreaUtils.getArea(id).getName());
            });
            jobInfoRespVO.setJobCity(cityNames);
            // 获取对应岗位的投递数量jobDeliveryNum
            Integer num = jobResumeInfoMapper.getJobDeliveryNum(jobInfo.getJobId());
            jobInfoRespVO.setJobDeliveryNum(num);
            result.add(jobInfoRespVO);
        }
        // 创建一个新的PageResult<JobInfoRespVO>对象，将result作为新的list
        return new PageResult<>(result, jobInfoPage.getTotal());
    }

    @Override
    public void updateExpiredStatus() {
        // 查询所有已发布职位信息
        List<JobInfoDO> jobInfoList = jobInfoMapper.selectList(new LambdaQueryWrapper<JobInfoDO>()
                .eq(JobInfoDO::getJobStatus, JobInfoEunums.JOB_STATUS_PUBLIC.getCode()));
        // 查看职位的信息是否过期，过期修改状态
        jobInfoList.forEach(jobInfo -> {
            if (jobInfo.getJobEndDay().isBefore(LocalDate.now())) {
                jobInfo.setJobStatus(JobInfoEunums.JOB_STATUS_EXPIRED.getCode());
                jobInfoMapper.updateById(jobInfo);
            }
        });
    }

}